<?php
/**
 * 用户管理模块
 */

namespace app\controllers\marketing;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\components\EmailMarket;
use yii\data\Pagination;
use app\components\TemplateMarket;
use app\models\marketing\GroupConversionStatistics;
use app\models\marketing\Statistics;
class StatisticsController extends \app\base\Controller
{
    public $enableCsrfValidation = false;
    
    public $channelArr = [
        0 => '全部',
        2 => 'emarsys',
        4 => 'cheet',
        6 => 'emarsys_new',
        5 => 'benchmark',
        9 => 'webpower'
    ];
    
    
    /*
     *  邮件分组转化统计
     *  @liugang 2018-2-7
     */
    public function actionGroupConversionStatistics() {
        $data = [];
        $siteInfo = [];
        
        $defaultPageSize = 20;

        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        $site = Yii::$app->request->$mothod('site');
        $showBox = Yii::$app->request->$mothod('show_box');
        $showBox = $showBox ? trim($showBox) : 'formList';

        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();

        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        if (empty($siteInfo)) {
            exit('你好，你没有任何网站的相关权限');
        }
        
        // 当前用户所有有权限的站点结束
        $formData['siteInfo'] = $siteInfo;
        
        $defaultSiteId = array_keys($siteInfo,current($siteInfo));

        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : $defaultSiteId[0]; // 当前选择的网站
        // 统计日期相关
        if (isset($formData['reservation_time'])) {
            if (!empty($formData['reservation_time'])) {
                $reservationTimeArr = explode(' - ', $formData['reservation_time']);
                $formData['startTime'] = $reservationTimeArr[0];
                $formData['endTime'] = $reservationTimeArr[1];
            } else {
                $formData['startTime'] = '';
                $formData['endTime'] = '';
            }
        } else {
            // 当前时间 
            $curTime = \yii::$app->bjTime->time();
            $curTime30DaysAgo = $curTime - 30 * 24 * 3600; // 30天之前的时间戳

            $time30DaysStart = \yii::$app->bjTime->date('Y-m-d', $curTime30DaysAgo);
            $timeCurStart = \yii::$app->bjTime->date('Y-m-d', $curTime);

            $formData['startTime'] = !empty($formData['startTime']) ? $formData['startTime'] : $time30DaysStart;
            $formData['endTime'] = !empty($formData['endTime']) ? $formData['endTime'] : $timeCurStart;
        }
        // 获取邮件类型
        if(!empty($formData['mail_business']) && $formData['mail_business'] == 1) {
            $templateTypeArrInfo[0] = "全部";
            $formData['tempType'] = $templateTypeArrInfo;
        } else {
            $TemplateMarket = new TemplateMarket();
            if (!empty($formData['site_id'])) {
                $templateTypeArr = $TemplateMarket->getTemplateType($formData['site_id']);
            } else {
                $templateTypeArr = $TemplateMarket->getTemplateType();
            }

            $templateTypeArrInfo[0] = "全部";
            if (isset($templateTypeArr) && is_array($templateTypeArr)) {
                foreach ($templateTypeArr as $templateTypeArrK => $templateTypeArrV) {
                    $templateTypeArrInfo[$templateTypeArrV['id']] = $templateTypeArrV['template_type_name']; 
                } 
                $formData['tempType'] = $templateTypeArrInfo;
            }
        }
        if(!empty($formData['mail_business']) && $formData['mail_business'] == 1 && $formData['site_id'] == 12) {
            // 渠道来源
            $reg_source = TemplateMarket::regSourceList($formData['site_id']);
        } else {
            $reg_source = ['ALL' => '全站'];
        }
        
        
        
        // 邮件业务
        $mailBusinessType[1] = "系统邮件";
        $mailBusinessType[2] = "营销邮件";
        $formData['mailBusinessType'] = $mailBusinessType;
        $formData['temp_type_id'] = isset($formData['temp_type_id']) ? $formData['temp_type_id'] : 0;
        $formData['mail_business'] = isset($formData['mail_business']) ? $formData['mail_business'] : 2;
        $formData['template_name'] = isset($formData['template_name']) ? $formData['template_name'] : '';
        $formData['reg_source'] = isset($formData['reg_source']) ? trim($formData['reg_source']) : 'ALL';
        
        $page = Yii::$app->request->$mothod('page') ? Yii::$app->request->$mothod('page') : 1;
        $pageSize = Yii::$app->request->$mothod('pagesize') ? Yii::$app->request->$mothod('pagesize') : $defaultPageSize;
        
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
        
        $statistics = new GroupConversionStatistics();
        
        if(isset($formData['act']) && $formData['act'] == 'export') { // 导出文件
            // 导出数据到文件
            $data = $statistics->exportGroupConversionData($formData,$templateTypeArrInfo); // 获取每天的发送量数据
            
            if($data['code'] != 0) {
                $this->sysMsg('警告',$data['msg'],$params = [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
            }
            return;
            
        } else {
            // 查询数据
            $data = $statistics->getGroupConversionData($formData,$templateTypeArrInfo); // 获取每天的发送量数据
            //
            $analysis = $formData;
            $analysis['pageSize'] = 10000;
            $analysis['page'] = 1;
            $analysisData = $statistics->getGroupConversionData($analysis,$templateTypeArrInfo); // 获取每天的发送量数据
           // echo "<pre/>";
           // print_r($analysisData);die;
            $analysisInfo = self::dataProcessing($analysisData);

            $searchCondition = '';
            if(isset($formData['reservation_time'])) {
                $searchCondition .= '&reservation_time='. $formData['reservation_time'];
            }
            if($formData['site_id']) {
                $searchCondition .= '&site_id='.$formData['site_id'];
            }
            
            if($formData['reg_source'] && $formData['reg_source'] != 'ALL') {
                $searchCondition .= '&amp;reg_source='.$formData['reg_source'];
            }
            
            if($formData['temp_type_id']) {
                $searchCondition .= '&temp_type_id='.$formData['temp_type_id'];
            }
            if($formData['template_name']) {
                $searchCondition .= '&template_name='.$formData['template_name'];
            }
            // 邮件业务
            if(!empty($formData['mail_business'])) {
               $searchCondition .= '&mail_business='.intval($formData['mail_business']);
            }
            $orderLink = [];
            
            // 送达率排序连接
            $orderLink['sendSuccessOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=sendSuccessOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'sendSuccessOrder' && $formData['order'] == 1) {
                $orderLink['sendSuccessOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=sendSuccessOrder&order=2'.$searchCondition;
            }
            // 打开率排序连接
            $orderLink['openOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=openOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'openOrder' && $formData['order'] == 1) {
                $orderLink['openOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=openOrder&order=2'.$searchCondition;
            }
            // 点击转化率排序连接
            $orderLink['clickOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=clickOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'clickOrder' && $formData['order'] == 1) {
                $orderLink['clickOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=clickOrder&order=2'.$searchCondition;
            }
            // 退订投诉率排序连接
            $orderLink['unsubscribeOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=unsubscribeOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'unsubscribeOrder' && $formData['order'] == 1) {
                $orderLink['unsubscribeOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=unsubscribeOrder&order=2'.$searchCondition;
            }
            // 退回率排序连接
            $orderLink['bouncesOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=bouncesOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'bouncesOrder' && $formData['order'] == 1) {
                $orderLink['bouncesOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=bouncesOrder&order=2'.$searchCondition;
            }
            
            // 下单转化率排序
            $orderLink['orderTransformationLvOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=orderTransformationLvOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'orderTransformationLvOrder' && $formData['order'] == 1) {
                $orderLink['orderTransformationLvOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=orderTransformationLvOrder&order=2'.$searchCondition;
            }
            
            // 下单率排序
            $orderLink['orderLvOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=orderLvOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'orderLvOrder' && $formData['order'] == 1) {
                $orderLink['orderLvOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=orderLvOrder&order=2'.$searchCondition;
            }
            
            // 下单金额排序
            $orderLink['orderMoneyOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=orderMoneyOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'orderMoneyOrder' && $formData['order'] == 1) {
                $orderLink['orderMoneyOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=orderMoneyOrder&order=2'.$searchCondition;
            }
            
            // 已支付下单数排序
            $orderLink['payedOrderNumsOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=payedOrderNumsOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'payedOrderNumsOrder' && $formData['order'] == 1) {
                $orderLink['payedOrderNumsOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=payedOrderNumsOrder&order=2'.$searchCondition;
            }
            
            // 已支付下单金额排序
            $orderLink['payedOrderMoneyOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=payedOrderMoneyOrder&order=1'.$searchCondition;
            if(isset($formData['order_name']) && $formData['order_name'] == 'payedOrderMoneyOrder' && $formData['order'] == 1) {
                $orderLink['payedOrderMoneyOrder'] = '/marketing/statistics/group-conversion-statistics?site=marketing_email&order_name=payedOrderMoneyOrder&order=2'.$searchCondition;
            }
            
            // 分页相关
            $pages = new Pagination(['totalCount' =>$data['totalCount'], 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
            // echo "<pre/>";
            // print_r($analysisInfo);die;
            return $this->render('//marketing//statistics/group-conversion-statistics',[
                'data' => $data['group_info'],
                'formData' => $formData,
                'orderLink'  => $orderLink,
                'site'    => $site,
                'pages' => $pages,
                'reg_source' => $reg_source,
                'analysisInfo' => $analysisInfo,
                'showBox' => $showBox,
            ]);
        } 
    }

    /**
     * @return string
     * @author xianxuefeng
     * @date 2018-7-21
     */
    private function dataProcessing($info) {
        $res = [
             'send_success_nums' =>[],   // 发送成功数
             'open_count' =>[],           // 打开数
             'click_count' =>[],     // 点击数
             'order_user_nums' =>[],     // 下单用户数
             'complaint_unsubscribe' =>[],  // 退订投诉数
            'send_success_rate' =>[],   // 送达率
            'open_rate' =>[],           // 打开率
            'click_rate' =>[],     // 点击转化率
            'order_transformation_lv' =>[],     // 下单转化率
            'complaint_unsubscribe_rate' =>[],  // 退订投诉率
            'template_name' =>[],  //邮件名
        ];

        if(empty($info['group_info']) || !  isset($info['group_info'])) {
            return $res;
        }
//        echo "<pre/>";
//        print_r($info['group_info']);die;
        foreach ($info['group_info'] as $val) {
            $res['send_success_nums'][] = isset($val['send_success_nums']) ? $val['send_success_nums'] : 0 ;
            $res['open_count'][] = isset($val['open_count']) ? $val['open_count'] : 0 ;
            $res['click_count'][] = isset($val['click_count']) ? $val['click_count'] : 0 ;
            $res['order_user_nums'][] = isset($val['order_user_nums']) ? $val['order_user_nums'] : 0 ;
            $res['complaint_unsubscribe'][] = isset($val['complaint_unsubscribe']) ? $val['complaint_unsubscribe'] : 0 ;
            $res['send_success_rate'][] = isset($val['send_success_rate']) ? $val['send_success_rate']*100 : 0 ;
            $res['open_rate'][] = isset($val['open_rate']) ? $val['open_rate']*100 : 0 ;
            $res['click_rate'][] = isset($val['click_rate']) ? $val['click_rate']*100 : 0 ;
            $res['order_transformation_lv'][] = isset($val['order_transformation_lv']) ? $val['order_transformation_lv'] : 0 ;
            $res['complaint_unsubscribe_rate'][] = isset($val['complaint_unsubscribe_rate']) ? $val['complaint_unsubscribe_rate'] : 0 ;
            $res['template_name'][] = isset($val['template_name']) ? "'{$val['template_name']}'" : '0';
//          $res['template_name'][] = isset($val['template_name']) ? $val['template_name'] ."," : 0 ."," ;
        //  $res['template_name'] = "1,2,3" ;
        }
//        echo "<pre/>";

        $res['send_success_nums'] = join(",",$res['send_success_nums']);
        $res['open_count'] = join(",",$res['open_count']);
        $res['click_count'] = join(",",$res['click_count']);
        $res['order_user_nums'] = join(",",$res['order_user_nums']);
        $res['complaint_unsubscribe'] = join(",",$res['complaint_unsubscribe']);
        $res['send_success_rate'] = join(",",$res['send_success_rate']);
        $res['open_rate'] = join(",",$res['open_rate']);
        $res['click_rate'] = join(",",$res['click_rate']);
        $res['order_transformation_lv'] = join(",",$res['order_transformation_lv']);
        $res['complaint_unsubscribe_rate'] = join(",",$res['complaint_unsubscribe_rate']);
        $res['template_name'] = join(",",$res['template_name']);
        return $res;
    }


    /*
     *  站群邮件转化统计
     *  @liugang 2018-2-7
     */
    public function actionStationGroupConversionStatistics() {
        $data = [];
        $siteInfo = [];
        
        $defaultPageSize = 20;
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        $site = Yii::$app->request->$mothod('site');
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        $siteInfo[0] = '全部';
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        if (empty($siteInfo)) {
            exit('你好，你没有任何网站的相关权限');
        }
        // 当前用户所有有权限的站点结束
        $formData['siteInfo'] = $siteInfo;

        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0; // 当前选择的网站
        // 统计日期相关
        if (isset($formData['reservation_time'])) {
            if (!empty($formData['reservation_time'])) {
                $reservationTimeArr = explode(' - ', $formData['reservation_time']);
                $formData['startTime'] = $reservationTimeArr[0];
                $formData['endTime'] = $reservationTimeArr[1];
            } else {
                $formData['startTime'] = '';
                $formData['endTime'] = '';
            }
        } else {
            // 当前时间 
            $curTime = \yii::$app->bjTime->time();
            $curTime30DaysAgo = $curTime - 30 * 24 * 3600; // 30天之前的时间戳

            $time30DaysStart = \yii::$app->bjTime->date('Y-m-d', $curTime30DaysAgo);
            $timeCurStart = \yii::$app->bjTime->date('Y-m-d', $curTime);

            $formData['startTime'] = !empty($formData['startTime']) ? $formData['startTime'] : $time30DaysStart;
            $formData['endTime'] = !empty($formData['endTime']) ? $formData['endTime'] : $timeCurStart;
        }
        
        if(!empty($formData['mail_business']) && $formData['mail_business'] == 1) {
            $templateTypeArrInfo[0] = "全部";
            $formData['tempType'] = $templateTypeArrInfo;
        } else {
            // 获取邮件类型
            $TemplateMarket = new TemplateMarket();
            if (!empty($formData['site_id'])) {
                $templateTypeArr = $TemplateMarket->getTemplateType($formData['site_id']);
            } else {
                $templateTypeArr = $TemplateMarket->getTemplateType();
            }

            $templateTypeArrInfo[0] = "全部";
            if (isset($templateTypeArr) && is_array($templateTypeArr)) {
                foreach ($templateTypeArr as $templateTypeArrK => $templateTypeArrV) {
                    $templateTypeArrInfo[$templateTypeArrV['id']] = $templateTypeArrV['template_type_name']; 
                } 
                $formData['tempType'] = $templateTypeArrInfo;
            }
        
        }
        
          // 邮件业务
        $mailBusinessType[0] = "全部";
        $mailBusinessType[1] = "系统邮件";
        $mailBusinessType[2] = "营销邮件";
        $formData['mailBusinessType'] = $mailBusinessType;
        $formData['mail_business'] = isset($formData['mail_business']) ? $formData['mail_business'] : 0;
        $formData['temp_type_id'] = isset($formData['temp_type_id']) ? $formData['temp_type_id'] : 0;
        
        if(!empty($formData['mail_business']) && $formData['mail_business'] == 1 && $formData['site_id'] == 12) {
            // 渠道来源
            $reg_source = TemplateMarket::regSourceList($formData['site_id']);
        } else {
            $reg_source = ['ALL' => '全站'];
        }
        
        
        $formData['reg_source'] = isset($formData['reg_source']) ? trim($formData['reg_source']) : 'ALL';
        
        $page = Yii::$app->request->$mothod('page') ? Yii::$app->request->$mothod('page') : 1;
        $pageSize = Yii::$app->request->$mothod('pagesize') ? Yii::$app->request->$mothod('pagesize') : $defaultPageSize;
        
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
      
        // 查询数据
        $statistics = new GroupConversionStatistics();
        $data = $statistics->getStationGroupConversionData($formData); // 获取每天的发送量数据 
        
        $searchCondition = '';
        if(isset($formData['reservation_time'])) {
            $searchCondition .= '&reservation_time='. $formData['reservation_time'];
        }
        if($formData['site_id']) {
            $searchCondition .= '&site_id='.$formData['site_id'];
        }
        
        if($formData['reg_source'] && $formData['reg_source'] != 'ALL') {
            $searchCondition .= '&amp;reg_source='.$formData['reg_source'];
        }
            
        if($formData['temp_type_id']) {
            $searchCondition .= '&temp_type_id='.$formData['temp_type_id'];
        }
        
        // 邮件业务
        if(!empty($formData['mail_business'])) {
           $searchCondition .= '&mail_business='.intval($formData['mail_business']);
        }
        
        $orderLink = [];

        // 送达率排序连接
        $orderLink['sendSuccessOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=sendSuccessOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'sendSuccessOrder' && $formData['order'] == 1) {
            $orderLink['sendSuccessOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=sendSuccessOrder&order=2'.$searchCondition;
        }
        // 打开率排序连接
        $orderLink['openOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=openOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'openOrder' && $formData['order'] == 1) {
            $orderLink['openOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=openOrder&order=2'.$searchCondition;
        }
        // 点击转化率排序连接
        $orderLink['clickOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=clickOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'clickOrder' && $formData['order'] == 1) {
            $orderLink['clickOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=clickOrder&order=2'.$searchCondition;
        }
        // 退订投诉率排序连接
        $orderLink['unsubscribeOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=unsubscribeOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'unsubscribeOrder' && $formData['order'] == 1) {
            $orderLink['unsubscribeOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=unsubscribeOrder&order=2'.$searchCondition;
        }
        // 退回率排序连接
        $orderLink['bouncesOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=bouncesOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'bouncesOrder' && $formData['order'] == 1) {
            $orderLink['bouncesOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=bouncesOrder&order=2'.$searchCondition;
        }
        
        // 下单转化率排序
        $orderLink['orderTransformationLvOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=orderTransformationLvOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'orderTransformationLvOrder' && $formData['order'] == 1) {
            $orderLink['orderTransformationLvOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=orderTransformationLvOrder&order=2'.$searchCondition;
        }

        // 下单率排序
        $orderLink['orderLvOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=orderLvOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'orderLvOrder' && $formData['order'] == 1) {
            $orderLink['orderLvOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=orderLvOrder&order=2'.$searchCondition;
        }

        // 下单金额排序
        $orderLink['orderMoneyOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=orderMoneyOrder&order=1'.$searchCondition;
        if(isset($formData['order_name']) && $formData['order_name'] == 'orderMoneyOrder' && $formData['order'] == 1) {
            $orderLink['orderMoneyOrder'] = '/marketing/statistics/station-group-conversion-statistics?site=marketing_email&order_name=orderMoneyOrder&order=2'.$searchCondition;
        }
            
        
        // 分页相关
        $pages = new Pagination(['totalCount' =>$data['totalCount'], 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
        
        return $this->render('//marketing//statistics/station-group-conversion-statistics',[
            'data' => $data['group_info'],
            'total' => $data['total_info'],
            'formData' => $formData,
            'orderLink'  => $orderLink,
            'reg_source' => $reg_source,
            'site'    => $site,
            'pages' => $pages,
        ]);
    }

    /**
     * 发送量统计
     */
    public function actionSendNumStatistics()
    {
        
        $data = [];
        $siteInfo = [];
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        $site = Yii::$app->request->$mothod('site');
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        $siteInfo[0] = '全部';
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        if (empty($siteInfo)) {
            exit('你好，你没有任何网站的相关权限');
        }
        
        // 当前用户所有有权限的站点结束
        $formData['siteInfo'] = $siteInfo;
       
        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0; // 当前选择的网站
        
         // 统计日期相关
        if (isset($formData['reservation_time'])) {
            if (!empty($formData['reservation_time'])) {
                $reservationTimeArr = explode(' - ',$formData['reservation_time']);
                $formData['startTime'] = $reservationTimeArr[0];
                $formData['endTime'] = $reservationTimeArr[1];
            } else {
                $formData['startTime'] = '';
                $formData['endTime'] = '';
            }
        } else {
            // 当前时间 
            $curTime = \yii::$app->bjTime->time();
            $curTime30DaysAgo = $curTime - 30*24*3600; // 30天之前的时间戳
           
            $time30DaysStart = \yii::$app->bjTime->date('Y-m-d',$curTime30DaysAgo);
            $timeCurStart = \yii::$app->bjTime->date('Y-m-d',$curTime);
            
            $formData['startTime'] = !empty($formData['startTime']) ? $formData['startTime'] : $time30DaysStart;
            $formData['endTime'] = !empty($formData['endTime']) ? $formData['endTime'] : $timeCurStart;
        }
        
        // 所有发送通道
        $formData['channelArr'] = $this->channelArr;
        
        $formData['channel_id'] = isset($formData['channel_id']) ? $formData['channel_id'] : 0;
        
        
        // 邮箱类型
        $formData['mailType'] = [];
        $formData['mailType'][0] = "全部";
        $emailObj = new EmailMarket();
        $mailType = $emailObj->getAllEmailTypeInfo();
        foreach ($mailType as $mailTypeV) {
            $formData['mailType'][$mailTypeV['id']] = $mailTypeV['mail_name'];
        }
        $formData['mail_type_id'] = isset($formData['mail_type_id']) ? $formData['mail_type_id'] : 0;
        
        // 获取邮件类型
        $TemplateMarket = new TemplateMarket();
        if (!empty($formData['site_id'])) {
            $templateTypeArr = $TemplateMarket->getTemplateType($formData['site_id']);
        } else {
            $templateTypeArr = $TemplateMarket->getTemplateType();
        }
        //$templateTypeArr = $TemplateMarket->getTemplateType();
        $templateTypeArrInfo[0] = "全部";
        if (isset($templateTypeArr) && is_array($templateTypeArr)) {
            foreach ($templateTypeArr as $templateTypeArrK => $templateTypeArrV) {
                $templateTypeArrInfo[$templateTypeArrV['id']] = $templateTypeArrV['template_type_name']; 
            } 
            $formData['tempType'] = $templateTypeArrInfo;
        }
        $formData['temp_type_id'] = isset($formData['temp_type_id']) ? $formData['temp_type_id'] : 0;
        
        // 查询数据
        $statistics = new Statistics();
        $dataByDay = $statistics->getEverydaySendNumData($formData); // 获取每天的发送量数据 
        
       
        $day = "'" . join("','", $dataByDay['day']) . "'";
        $sendNums = join(",", $dataByDay['send_nums']);
        $deliveryLv = join(",", $dataByDay['delivery_lv']);
        
       
        // 获取每天的汇总数据
        $collectData = $statistics->getSendNumCollectData($formData); 
        
        return $this->render('//marketing//statistics/send-num-statistics',[
            'data' => $data,
            'formData' => $formData,
            'day' => $day,
            'sendNums' => $sendNums,
            'deliveryLv' => $deliveryLv,
            'collectData' => $collectData,
        ]);
    } 
    
    
    /**
     * 发送质量分析
     */
    public function actionSendQualityAnalyze()
    {
        $data = [];
        $siteInfo = [];
        $mothod = strtolower(Yii::$app->request->getMethod());
         $formData = Yii::$app->request->$mothod();
        $site = Yii::$app->request->$mothod('site');
         // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        $siteInfo[0] = '全部';
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        if (empty($siteInfo)) {
            exit('你好，你没有任何网站的相关权限');
        }
     
        // 当前用户所有有权限的站点结束
        $formData['siteInfo'] = $siteInfo;
       
        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0; // 当前选择的网站
        
        // 统计日期相关
        if (isset($formData['reservation_time'])) {
            if (!empty($formData['reservation_time'])) {
                $reservationTimeArr = explode(' - ',$formData['reservation_time']);
                $formData['startTime'] = $reservationTimeArr[0];
                $formData['endTime'] = $reservationTimeArr[1];
            } else {
                $formData['startTime'] = '';
                $formData['endTime'] = '';
            }
        } else {
            // 当前时间 
            $curTime = \yii::$app->bjTime->time();
            $curTime30DaysAgo = $curTime - 30*24*3600; // 30天之前的时间戳
           
            $time30DaysStart = \yii::$app->bjTime->date('Y-m-d',$curTime30DaysAgo);
            $timeCurStart = \yii::$app->bjTime->date('Y-m-d',$curTime);
            
            $formData['startTime'] = !empty($formData['startTime']) ? $formData['startTime'] : $time30DaysStart;
            $formData['endTime'] = !empty($formData['endTime']) ? $formData['endTime'] : $timeCurStart;
        }
        
        // 所有发送通道
        $formData['channelArr'] = $this->channelArr;
        
        $formData['channel_id'] = isset($formData['channel_id']) ? $formData['channel_id'] : 0;
        
        
        // 邮箱类型
        $formData['mailType'] = [];
        $formData['mailType'][0] = "全部";
        $emailObj = new EmailMarket();
        $mailType = $emailObj->getAllEmailTypeInfo();
        foreach ($mailType as $mailTypeV) {
            $formData['mailType'][$mailTypeV['id']] = $mailTypeV['mail_name'];
        }
        $formData['mail_type_id'] = isset($formData['mail_type_id']) ? $formData['mail_type_id'] : 0;
        
        // 获取邮件类型
        $TemplateMarket = new TemplateMarket();
        if (!empty($formData['site_id'])) {
            $templateTypeArr = $TemplateMarket->getTemplateType($formData['site_id']);
        } else {
            $templateTypeArr = $TemplateMarket->getTemplateType();
        }
        // $templateTypeArr = $TemplateMarket->getTemplateType();
        $templateTypeArrInfo[0] = "全部";
        if (isset($templateTypeArr) && is_array($templateTypeArr)) {
            foreach ($templateTypeArr as $templateTypeArrK => $templateTypeArrV) {
                $templateTypeArrInfo[$templateTypeArrV['id']] = $templateTypeArrV['template_type_name']; 
            } 
            $formData['tempType'] = $templateTypeArrInfo;
        }
        $formData['temp_type_id'] = isset($formData['temp_type_id']) ? $formData['temp_type_id'] : 0;
        
       
        // 查询相关数据
        $statistics = new Statistics();
        $dataByDay = $statistics->getEverydayData($formData); // 柱状图信息
        $pieData = $statistics->getPieData($formData);
        
        // 数据处理
        $day="";
        $dayArr = [];
        $hardBouncesNums = "";
        $hardBouncesNumsArr = [];
        $softBouncesNums = "";
        $softBouncesNumsArr = [];
        $complaintNums = "";
        $complaintNumsArr = [];
        $unsubscribeNums = "";
        $unsubscribeNumsArr = [];
        foreach ($dataByDay as $dataByDayK => $dataByDayV) {
            $dayArr[] = "'{$dataByDayV['day']}'"; 
            $hardBouncesNumsArr[] = $dataByDayV['hard_bounces_nums'];
            $softBouncesNumsArr[] = $dataByDayV['soft_bounces_nums'];
            $complaintNumsArr[] = $dataByDayV['complaint_nums'];
            $unsubscribeNumsArr[] = $dataByDayV['unsubscribe_nums'];
        }
        $day = join(",",$dayArr);
        $hardBouncesNums = join("," , $hardBouncesNumsArr);
        $softBouncesNums = join("," , $softBouncesNumsArr);
        $complaintNums = join("," , $complaintNumsArr);
        $unsubscribeNums = join("," , $unsubscribeNumsArr);
        
        return $this->render('//marketing//statistics/send-quality-analyze',[
            'data' => $data,
            'formData' => $formData,
            'day' => $day,
            'hardBouncesNums' => $hardBouncesNums,
            'softBouncesNums' => $softBouncesNums,
            'complaintNums' => $complaintNums,
            'unsubscribeNums' => $unsubscribeNums,
            
            'pieData' => $pieData,
        ]);
    } 
} 